import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:hktk_app/ienum/my/asset_type_enum.dart';
import 'package:hktk_app/ienum/my/rank_type_enum.dart';
import 'package:hktk_app/ienum/package/payment_type_enum.dart';
import 'package:hktk_app/ienum/subject/collection_type_enum.dart';
import 'package:hktk_app/model/industry/exam_model.dart';
import 'package:hktk_app/model/my/app_list_model.dart';
import 'package:hktk_app/model/my/asset_vo_model.dart';
import 'package:hktk_app/model/my/chaper_tree_model.dart';
import 'package:hktk_app/model/my/my_choose_category_model.dart';
import 'package:hktk_app/model/my/note_chapter_model.dart';
import 'package:hktk_app/model/my/version_model.dart';
import 'package:hktk_app/model/package/package_model.dart';
import 'package:hktk_app/model/page/resource_page_model.dart';
import 'package:hktk_app/model/subject/note_model.dart';
import 'package:hktk_app/model/subject/question_collection_model.dart';
import 'package:hktk_app/model/subject/upload_collection_model.dart';
import 'package:hktk_app/util/dao_util.dart';

/// 我的相关接口
class MyDao {
  // industryCode：行业id，
  // examId：考试id，
  // categoryId：科目id，
  // current：页数，
  // size：条数，
  /// 获取科目下最新错题
  static Future<ResourcePageModel<UploadCollectionModel>> categoryNewsError({
    required String industryCode,
    required String examId,
    required int categoryId,
    required int current,
    int size = 20,
  }) async {
    String url =
        "/exam/records/category-news-error?industryCode=$industryCode&examId=$examId&categoryId=$categoryId&current=$current&size=$size";
    // 发送请求
    var data = await httpGET(url: url, tips: "获取科目下最新错题");
    return ResourcePageModel.fromJson(data);
  }

  // industryCode：行业id，
  // examId：考试id，
  // categoryId：科目id，
  // current：页数，
  // size：条数，
  /// 获取科目下的最新收藏
  static Future<ResourcePageModel<UploadCollectionModel>> categoryNewsCollect({
    required String industryCode,
    required String examId,
    required int categoryId,
    required int current,
    int size = 20,
  }) async {
    String url =
        "/exam/records/category-news-collect?industryCode=$industryCode&examId=$examId&categoryId=$categoryId&current=$current&size=$size";
    // 发送请求
    var data = await httpGET(url: url, tips: "获取科目下最新错题");
    return ResourcePageModel.fromJson(data);
  }

  // industryCode：行业id，
  // examId：考试id，
  // categoryId：科目id，
  // collectionType：类型，
  // chapterId：章节id，没有父id不传，
  // rankType：是错题/收藏，
  // industryName：行业名称，
  // examName：考试名称，
  // categoryName：科目名称，
  // columnId：栏目id，
  /// 获取集合试题的做题记录归类
  static Future<List<ChapterTreeModel>> chapterRecordsTree({
    required String industryCode,
    required String examId,
    required int categoryId,
    required CollectionTypeEnum collectionType,
    String? chapterId,
    required RankTypeEnum type,
    String? industryName,
    String? examName,
    String? categoryName,
    String? columnId,
  }) async {
    String url =
        "/exam/records/chapter-records-tree?industryCode=$industryCode&examId=$examId&categoryId=$categoryId&collectionType=${collectionType.name}&type=${type.name}";
    if (chapterId != null) {
      url = "$url&chapterId=$chapterId";
    }
    if (industryName != null) {
      url = "$url&industryName=$industryName";
    }
    if (examName != null) {
      url = "$url&examName=$examName";
    }
    if (categoryName != null) {
      url = "$url&categoryName=$categoryName";
    }
    if (columnId != null) {
      url = "$url&columnId=$columnId";
    }
    // 发送请求
    var data = await httpGET(url: url, tips: "获取集合试题的做题记录归类");
    if (data is List<dynamic>) {
      return data.map((v) => ChapterTreeModel.fromJson(v)).toList();
    }
    return <ChapterTreeModel>[];
  }

  /// 删除科目错题记录
  static Future<void> delCategoryError({
    required String industryCode,
    required String examId,
    required int categoryId,
  }) async {
    Map<String, dynamic> params = {
      "industryCode": industryCode,
      "examId": examId,
      "categoryId": categoryId,
    };
    String url = "/exam/records/del-category-error";
    // 发送请求
    await httpDelete(
      url: url,
      params: params,
      tips: "删除科目错题记录",
    );
  }

  /// 删除科目收藏记录
  static Future<void> delCategoryCollect({
    required String industryCode,
    required String examId,
    required int categoryId,
  }) async {
    Map<String, dynamic> params = {
      "industryCode": industryCode,
      "examId": examId,
      "categoryId": categoryId,
    };
    String url = "/exam/records/del-category-collect";
    // 发送请求
    await httpDelete(
      url: url,
      params: params,
      tips: "删除科目收藏记录",
    );
  }

  // industryCode：行业id，
  // examId：考试id，
  // categoryId：科目id，
  // collectId：集合id，
  // collectionType：类型，
  // type：收藏/错题类型，
  /// 获取收藏试题集合
  static Future<QuestionCollectionModel> collectCollectionNew({
    required String industryCode,
    required String examId,
    required int categoryId,
    required String collectId,
    required CollectionTypeEnum collectionType,
    required RankTypeEnum type,
  }) async {
    Map<String, dynamic> params = {
      "industryCode": industryCode,
      "examId": examId,
      "categoryId": categoryId,
      "collectId": collectId,
      "collectionType": collectionType.name,
      "type": type.name,
    };
    String url = "/exam/records/collect-collection-new";
    // 发送请求
    var data = await httpPOST(
      url: url,
      params: params,
      tips: "获取收藏试题集合",
    );
    return QuestionCollectionModel.fromJson(data);
  }

  // current：页数，
  // size：条数，
  // categoryId：科目id，
  // newFlag：是否最新笔记,则不用传type，
  // type：chapter章节,paper试卷, 其他的传rankError，
  /// 我的笔记
  static Future<dynamic> myNote({
    required int current,
    required int size,
    required int categoryId,
    required bool newFlag,
    CollectionTypeEnum? type,
  }) async {
    String url =
        "/exam/note/my-note?current=$current&size=$size&categoryId=$categoryId&newFlag=$newFlag";
    if (type != null) {
      url = "$url&type=${type.name}";
    }
    // 发送请求
    var data = await httpGET(url: url, tips: "我的笔记");

    if (type != null) {
      ResourcePageModel<NoteChapterModel> resPage;
      if (data != null) {
        resPage = ResourcePageModel.fromJson(data);
      } else {
        resPage = ResourcePageModel();
      }
      return resPage;
    } else {
      ResourcePageModel<NoteModel> resPage;
      if (data != null) {
        resPage = ResourcePageModel.fromJson(data);
      } else {
        resPage = ResourcePageModel();
      }
      return resPage;
    }
  }

  // relationId：关联id，
  // chapterFlag：是否是章节，
  // categoryId：科目id，
  /// 根据关联id找笔记
  static Future<List<NoteModel>> noteByRelation({
    required String relationId,
    required bool chapterFlag,
    required String categoryId,
  }) async {
    String url =
        "/exam/note/note-by-relation?relationId=$relationId&chapterFlag=$chapterFlag&categoryId=$categoryId";
    // 发送请求
    var data = await httpGET(url: url, tips: "根据关联id找笔记");
    if (data is List<dynamic>) {
      return data.map((v) => NoteModel.fromJson(v)).toList();
    }
    return <NoteModel>[];
  }

  /// 我购买的考试
  static Future<List<ExamModel>> myBuyExam() async {
    String url = "/order/user-assets/my-buy-exam";
    // 发送请求
    var data = await httpGET(url: url, tips: "我购买的考试");
    if (data is List<dynamic>) {
      return data.map((v) => ExamModel.fromJson(v)).toList();
    }
    return <ExamModel>[];
  }

  /// 我购买的资源
  static Future<List<AssetVoModel>> myBuyAsset({
    required AssetTypeEnum type,
    required String categoryIds,
  }) async {
    String url =
        "/order/user-assets/my-buy-asset?type=${type.name}&categoryIds=$categoryIds";
    // 发送请求
    var data = await httpGET(url: url, tips: "我购买的资源");
    if (data is List<dynamic>) {
      return data.map((v) => AssetVoModel.fromJson(v)).toList();
    }
    return <AssetVoModel>[];
  }

  /// 用户激活码开课
  static Future<SkuModel> openSkuCode({
    required String code,
  }) async {
    Map<String, dynamic> params = {"code": code};
    String url = "/order/or/order/user/open-sku-code";
    // 发送请求
    var data = await httpPOST(
      url: url,
      params: params,
      tips: "用户激活码开课",
    );
    if (data != null) {
      return SkuModel.fromJson(data);
    }
    return SkuModel();
  }

  /// app 端热词搜索
  static Future<List<AppListModel>> appList({
    int size = 10,
    String? name,
  }) async {
    String url = "/user/sys/hot-question/app-list?size=$size&type=normal";
    if (name != null) {
      url = "$url&name=$name";
    }
    // 发送请求
    var data = await httpGET(url: url, tips: "端热词搜索");
    if (data is List<dynamic>) {
      return data.map((v) => AppListModel.fromJson(v)).toList();
    }
    return <AppListModel>[];
  }

  /// 用户注销
  static Future<bool> logoff() async {
    String url = "/user/cu/member/logoff";
    // 发送请求
    var data = await httpDelete(url: url, params: {}, tips: "用户注销");
    return data;
  }

  /// 查找最新版本
  static Future<VersionModel> versionCurrent({
    required DeviceTypeEnum type,
  }) async {
    String url = "/version/current?type=${type.name}";
    // 发送请求
    var data = await httpGET(url: url, tips: "查找最新版本");
    if (data != null) {
      return VersionModel.fromJson(data);
    }
    return VersionModel();
  }

  /// 获取购买状态方法
  static Future<bool> versionCurrentIos() async {

    bool iosHide = false;
    // 判断支付方式
    if (Platform.isIOS) {
      // 加载版本方法
      VersionModel versionModel = await MyDao.versionCurrent(
        type: DeviceTypeEnum.ios,
      );

      // 判断是否需要隐藏对应支付方式
      if (versionModel.verify ?? false) {
        iosHide = true;
      }
    }
    return iosHide;
  }

  /// 删除科目所有记录
  static Future<bool> cleanCategory({
    required String industryCode,
    required String examId,
    required int categoryId,
  }) async {
    String url = "/exam/records/clean-category";
    Map<String, dynamic> params = {
      "industryCode": industryCode,
      "examId": examId,
      "categoryId": categoryId,
    };
    // 发送请求
    await httpDelete(
      url: url,
      params: params,
      tips: "删除科目所有记录",
    );
    return true;
  }
//   获取我的选择的科目
  static Future<UserChooseCategoryVo> getCurrentChoose() async {
    String url = "/exam/category/current-choose";
    // 发送请求
    var data = await httpGET(url: url, tips: "");
    return UserChooseCategoryVo.fromJson(data);
  }
  static Future<dynamic> getCalendar(int year,int month) async {
    String url = "/exam/calendar/get-calendar-list?year=$year&month=$month";
    // 发送请求
    return await httpGET(url: url, tips: "");
  }
}
